package com.itedge.solutionmanager.web.controller.task.impl;

import javax.annotation.PostConstruct;

import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itedge.infrastructure.service.domain.IProcessEntityService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.web.controller.task.impl.AbstractTaskController;
import com.itedge.infrastructure.web.taskhandler.ITaskDataHandler;
import com.itedge.infrastructure.domain.IProcessEntity;
import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.solutionmanager.taskdata.impl.customer.AddContactPersonTaskData;
import com.itedge.solutionmanager.taskdata.impl.customer.EditCustomerTaskData;
import com.itedge.solutionmanager.taskdata.impl.customer.FreezeCustomerTaskData;
import com.itedge.solutionmanager.taskdata.impl.customer.RemoveContactsTaskData;

/**
 * Implementation of task controller for {@link Customer} domain object
 * 
 * @author jhe
 *
 */
@RequestMapping("/customerTasks")
@Controller
public class CustomerTaskController extends AbstractTaskController {

	@Autowired
    private IProcessService<Customer> customerProcessService;	 
    @Autowired
    private IProcessEntityService<Customer> customerService;	
    @Autowired
    private ITaskDataHandler<EditCustomerTaskData, Customer> editCustomerTaskDataHandler;
    @Autowired 
    private ITaskDataHandler<AddContactPersonTaskData, Customer> addContactPersonTaskDataHandler;
    @Autowired 
    private ITaskDataHandler<RemoveContactsTaskData, Customer> removeContactsTaskDataHandler;
    @Autowired
    private ITaskDataHandler<FreezeCustomerTaskData, Customer> freezeCustomerTaskDataHandler;
	
	private static final String EDIT_CUSTOMER_TASK = "EDIT_CUSTOMER";
	private static final String ADD_CONTACT_TASK = "ASSIGN_PERSON";
	private static final String REMOVE_CONTACTS_TASK = "REMOVE_PERSONS";
	private static final String FREEZE_CUSTOMER_TASK = "FREEZE_CUSTOMER";
	
	@Autowired
    protected CustomerTaskController(MessageSource messageSource) {
		super(messageSource);
	}

	@Override
	@PostConstruct
	public void initializeMappings() {
		taskHandlerMapping.put(EDIT_CUSTOMER_TASK, editCustomerTaskDataHandler);
		taskHandlerMapping.put(ADD_CONTACT_TASK, addContactPersonTaskDataHandler);
		taskHandlerMapping.put(REMOVE_CONTACTS_TASK, removeContactsTaskDataHandler);
		taskHandlerMapping.put(FREEZE_CUSTOMER_TASK, freezeCustomerTaskDataHandler);
	}
	
	@Override
	protected Task getCurrentTask(String id) {
		return customerProcessService.getTaskById(id);
	}

	@Override
	protected HistoricTaskInstance getHistoryTask(String id) {
		return customerProcessService.getHistoryTaskById(id);
	}

	@Override
	protected IProcessEntity getLinkedObject(String procId) {
		return customerService.findEntityByProcessId(procId);
	}

	@Override
	protected String getTaskViewPath() {
		return "customerTasks/";
	}

	@Override
	protected String getLinkedObjectViewPath() {
		return "customers/";
	}

	@Override
	protected String getLinkedObjectAttrName() {
		return "customer";
	}
	
}
